package com.eastfair.home.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.home.entity.HomeServiceItem;
import com.eastfair.home.dto.HomeServiceItemSaveDTO;
import com.eastfair.home.dto.HomeServiceItemUpdateDTO;
import com.eastfair.home.dto.HomeServiceItemPageQuery;
import com.eastfair.home.service.HomeServiceItemService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.enumeration.DataIsolationSystemSceneEnum;
import com.eastfair.venuebooking.api.ServiceItemFeign;
import com.eastfair.venueservice.dto.ServiceItemDTO;
import com.eastfair.venueservice.dto.ServiceItemQuery;
import com.eastfair.venueservice.vo.ServiceItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * <p>
 * 前端控制器
 * 服务项
 * </p>
 *
 * @author linan
 * @date 2022-07-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/homeServiceItem")
@Api(value = "HomeServiceItem", tags = "服务项")
@PreAuth(replace = "home:homeServiceItem:")
public class HomeServiceItemController extends SuperController<HomeServiceItemService, Long, HomeServiceItem, HomeServiceItemPageQuery, HomeServiceItemSaveDTO, HomeServiceItemUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Resource
    private ServiceItemFeign serviceItemFeign;

    @Override
    public void handlerResult(IPage<HomeServiceItem> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<HomeServiceItem> homeServiceItemList = list.stream().map((map) -> {
            HomeServiceItem homeServiceItem = HomeServiceItem.builder().build();
            //TODO 请在这里完成转换
            return homeServiceItem;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(homeServiceItemList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<HomeServiceItemPageQuery> params) {
        HomeServiceItemPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 添加服务项
     *
     * @param serviceItemDTO 添加服务项
     * @return R
     */
    @ApiOperation(value = "添加服务项", notes = "添加服务项")
    @SysLog("添加服务项")
    @PostMapping("/addServiceItem")
    public R<Boolean> addServiceItem(@RequestBody ServiceItemDTO serviceItemDTO) {
        log.info("addServiceItem - 添加服务项 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.addServiceItem(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }

    /**
     * 修改服务项
     *
     * @param serviceItemDTO 修改服务项
     * @return R
     */
    @ApiOperation(value = "修改服务项", notes = "修改服务项")
    @SysLog("修改服务项")
    @PostMapping("/updateServiceItem")
    public R<Boolean> updateServiceItem(@RequestBody ServiceItemDTO serviceItemDTO) {
        log.info("addServiceItem - 修改服务项 serviceItemDTO={}", serviceItemDTO);
        try {
            return baseService.updateServiceItem(serviceItemDTO);
        } catch (Exception e) {
            return R.fail(e);
        }
    }



    /**
     * 上架配套服务
     * @param serviceItemDTO
     * @return
     */
    @ApiOperation(value = "上架配套服务")
    @SysLog("上架配套服务")
    @PostMapping(value = "/shelfService")
    public R<Boolean> shelfService(@RequestBody ServiceItemDTO serviceItemDTO){
        return baseService.shelfService(serviceItemDTO);
    }


    /**
     * 下架配套服务
     * @param serviceItemDTO
     * @return
     */
    @ApiOperation(value = "下架配套服务")
    @SysLog("下架配套服务")
    @PostMapping(value = "/offShelfService")
    public R<Boolean> offShelfService(@RequestBody ServiceItemDTO serviceItemDTO){
        return baseService.offShelfService(serviceItemDTO);
    }



    /**
     * 删除主场服务
     * @param serviceItemDTO
     * @return
     */
    @ApiOperation(value = "删除主场服务")
    @SysLog("删除主场服务")
    @PostMapping(value = "/delHomeServiceItemById")
    public R<Boolean> delHomeServiceItemById(@RequestBody ServiceItemDTO serviceItemDTO){
        return baseService.delHomeServiceItemById(serviceItemDTO);
    }

    /**
     * 主场服务项列表
     * @param params
     * @return
     */
    @ApiOperation(value = "主场服务项列表")
    @PostMapping(value = "/queryServiceItemList")
    public R<IPage<ServiceItemVO>> queryServiceItemList(@RequestBody PageParams<ServiceItemQuery> params) {
        IPage<ServiceItemVO> page = baseService.queryServiceItemList(params);
        return R.success(page);
    }

    /**
     * 同步服务
     *
     * @param serviceItemDTO 同步服务
     * @return R
     */
    @ApiOperation(value = "同步服务", notes = "同步服务")
    @PostMapping("/syncServiceItem")
    public R<Boolean> syncServiceItem(@RequestBody ServiceItemDTO serviceItemDTO){
        serviceItemDTO.setServiceProviderType(DataIsolationSystemSceneEnum.P_COMPOSITE_COURT.getCode());
        return serviceItemFeign.syncServiceItem(serviceItemDTO);
    }
}
